package cn.tedu.luban.demand.main.repository.impl;

import cn.tedu.luban.demand.main.converter.DemandConverter;
import cn.tedu.luban.demand.main.dao.DemandDAO;
import cn.tedu.luban.demand.main.repository.DemandRepository;
import cn.tedu.luban.demand.protocol.bo.RequestOrderBO;
import cn.tedu.luban.demand.protocol.dos.RequestOrderDO;
import cn.tedu.luban.demand.protocol.param.GrabOrderParam;
import cn.tedu.luban.demand.protocol.query.RequestOrderDBPagerQuery;
import cn.tedu.luban.demand.protocol.query.RequestOrderQuery;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DemandRepositoryImpl implements DemandRepository {
    @Autowired(required = false)
    private DemandDAO demandDAO;
    @Autowired
    private DemandConverter demandConverter;
    @Override
    public Long getRequestOrderCount(RequestOrderQuery requestOrderQuery) {
        RequestOrderDBPagerQuery dbQuery = demandConverter.toDbPagerQuery(requestOrderQuery);
        return demandDAO.countRequestOrder(dbQuery);
    }
    @Override
    public List<RequestOrderBO> queryRequestOrders(RequestOrderQuery requestOrderQuery) {
        RequestOrderDBPagerQuery dbQuery = demandConverter.toDbPagerQuery(requestOrderQuery);
        List<RequestOrderDO> poList = demandDAO.queryRequestOrders(dbQuery);
        return demandConverter.poList2BoList(poList);
    }

    public Integer grabOrder(GrabOrderParam grabOrderParam) {
        // version requestOrderNO
        // 将这个grabOrderParam转化成do对象
        return demandDAO.grabOrderCas(grabOrderParam);
    }

    @Override
    public RequestOrderBO getRequestOrder(String requestOrderNo) {
        //先查 do
        RequestOrderDO requestOrder = demandDAO.getRequestOrderByOrderNo(requestOrderNo);
        //在转化返回
        return demandConverter.po2bo(requestOrder);
    }

    @Override
    public Integer returnOrder(GrabOrderParam grabOrderParam) {
        return demandDAO.returnOrder(grabOrderParam);
    }
    @Override
    public RequestOrderBO getRequestOrderGrabed(String requestOrderNo) {
        RequestOrderDO reqeustOrder=demandDAO.selectReqeustOrderUnGrab(requestOrderNo);
        return demandConverter.po2bo(reqeustOrder);
    }

}
